package com.huluobo.a2109mouthdemo.addGroup;

import com.huluobo.a2109mouthdemo.base.BasePresenter;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCursorResult;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupOptions;

/**
 * Created by LC on 2023/12/4.
 */
public class AddGroupPresenter extends BasePresenter<AddGroupContract.View> implements AddGroupContract.Presenter {
    @Override
    public void createGroup(String groupName, String desc, String[] allMembers, String reason, EMGroupOptions option) {
        EMClient.getInstance().groupManager().asyncCreateGroup(groupName, desc, allMembers, reason, option,
                new EMValueCallBack<EMGroup>() {
                    @Override
                    public void onSuccess(EMGroup value) {
                        if (isAttach()) {
                            getView().onCreateGroupSuccess();
                        }
                    }

                    @Override
                    public void onError(int error, String errorMsg) {
                        if (isAttach()) {
                            getView().onCreateGroupFailed(errorMsg);
                        }
                    }
                });
    }

    @Override
    public void findGroup(int pageSize, String cursor) {
        EMClient.getInstance().groupManager().asyncGetPublicGroupsFromServer(pageSize, cursor,
                new EMValueCallBack<EMCursorResult<EMGroupInfo>>() {
                    @Override
                    public void onSuccess(EMCursorResult<EMGroupInfo> value) {
                        if (isAttach()) {
                            getView().findGroupSuccess(value.getData());
                        }
                    }

                    @Override
                    public void onError(int error, String errorMsg) {
                        if (isAttach()) {
                            getView().findGroupFailed(errorMsg);
                        }
                    }
                });
    }

    @Override
    public void joinGroup(String groupId) {
        EMClient.getInstance().groupManager().asyncJoinGroup(groupId, new EMCallBack() {
            @Override
            public void onSuccess() {
                if (isAttach()) {
                    getView().onJoinGroupSuccess();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (isAttach()) {
                    getView().onJoinGroupFailed(error,code);
                }
            }
        });
    }
}
